<?php
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Controller;
use think\Request;
class Pay extends controller
{
	public function payclass(){
		$zero_column=Db::name('column')->select();
        $this->view->assign('zero_column', $zero_column);
		$this->view->assign('Assets', Assets);
		return $this->view->fetch();
	}
	public function paylist($class){
		$class=input('class');
		if(!$class){
			$this->error('参数错误',url('product/plist'));
		}
		$chit=Db::name('chit');
		if($class==1 || $class==2){
			$where['carint']=$class;
			$list_shop=$chit->where($where)->order('id desc')->select();
			foreach ($list_shop as $k => $v) {
			$a_time = strtotime($v['addtime']);
			$b_time = strtotime('+'.$v['time'].'Day',$a_time);
			$list_shop[$k]['addtime']=date('Y-m-d',$a_time);
			$list_shop[$k]['overtime']=date('Y-m-d',$b_time);
			}
		}
		$shop=Db::name('integral_shop');
		if($class==3){
			$list_shop=$shop->where($where)->order('id desc')->select();
		}
		$zero_column=Db::name('column')->select();
        $this->view->assign('zero_column', $zero_column);
		$this->view->assign('list_shop', $list_shop);
		$this->view->assign('class', $class);
		$this->view->assign('Assets', Assets);
		return $this->view->fetch();
	}
	public function show()
	{
		$id=input('id');
		$class=input('class');
		if(!$id){
			$this->eror('参数错误',url('inex/index'));
		}
		if($class==1 || $class==2){
			$shop=Db::name('chit');
		}else if($class==3){
			$shop=Db::name('integral_shop');
		}else{
			$this->error('参数错误',url('index/index'));
		}

		$where['id']=$id;
		$shop=$shop->where($where)->find();
		foreach ($shop as $k) {
			$a_time = strtotime($shop['addtime']);
			$b_time = strtotime('+'.$shop['time'].'Day',$a_time);
			$shop['addtime']=date('Y-m-d',$a_time);
			$shop['overtime']=date('Y-m-d',$b_time);
			}
		$zero_column=Db::name('column')->select();
        $this->view->assign('zero_column', $zero_column);
		$this->view->assign('class', $class);
		$this->view->assign('Assets', Assets);
		$this->view->assign('shop', $shop);
		if($class==3){
			return $this->view->fetch('integral_shop');
		}
		return $this->view->fetch();
	}
	public function order()
	{
		
		//判断是否登录和验证手机号码是否存在
		$check=check_login();
		if($check=='0'){
			return 0;//没有登录
		}else if($check=='10'){
			return 10;//没有绑定手机
		}


		$userDb=Db::name('user');
		$openid=SESSION::get('wechat_user');
		$user=$userDb->where(array('openid'=>$openid))->find();

		

		$shopid=input('id');
		$shopdb=Db::name('chit');
		$shopinfo=$shopdb->find($shopid);
		$num=input('num');
		if($shopinfo['num'] < $num){
			return 100;//货存不足
		}

		$save['num']=$shopinfo['num']-$num;
		$where['id']=$shopid;
		$shopdb->where($where)->update($save);

		$order=Db::name('order');
		$data['orderid']='cmorder'.date('YmdHis',time()).substr($user['telphone'],-4).rand(0,9);
		$data['ordertime']=date('Y-m-d H:i:s',time());
		$data['state']='p0';//未付款
		$data['userid']=$user['id'];
		$data['type']=$shopinfo['carint'];
		$data['pay']=$shopinfo['pay'];
		$data['shopid']=$shopinfo['id'];
		$data['shoptitle']=$shopinfo['title'];
		$data['num']='1';
		$data['remake']='1';
		for ($i=0; $i < $num; $i++) { 
			$data['key']=time().substr($user['telphone'],-4).$i;
			$order -> insert($data);
			//echo $i;
		}
		

		$shop['money']=input('num')*$shopinfo['pay'];
		$shop['orderid']=$data['orderid'];
		
		SESSION::delete('cart');
		$cart=SESSION::get('cart');
		$cart[]=$shop;
		SESSION::set('cart',$cart);
		return 1;
	}
	public function success_pay()
	{
		$openid=SESSION::get('wechat_user');
    	$userDb=Db::name('user');
    	$user=$userDb->where(array('openid'=>$openid))->find();


		$orderid=input('orderid');
		$order=Db::name('order');

		$check=$order->where(array('orderid'=>$orderid,'userid'=>$user['id']))->find();

		if(!$check){
			return '1';
		}

		$where['orderid']=$orderid;
		$save['state']=1;
		$order->where($where)->update($save);
	}
	public function order_integral()
	{
		//判断是否登录和验证手机号码是否存在
		$check=check_login();
		if($check=='0'){
			return 0;//没有登录
		}else if($check=='10'){
			return 10;//没有绑定手机
		}

		//用户信息
		$userDb=Db::name('user');
		$openid=SESSION::get('wechat_user');
		$user=$userDb->where(array('openid'=>$openid))->find();
		$user['wxname']=base64_decode($user['username']);
		
		//判断货存
		$shopid=input('id');
		$shopdb=Db::name('integral_shop');
		$shopinfo=$shopdb->find($shopid);
		$num=input('num');
		if($shopinfo['num'] < $num){
			return 100;//货存不足
		}
		

		//扣除积分
		$take_out=$num*$shopinfo['pay'];
		$info=erp_active($user['telphone'],2,$take_out,$user['openid']);
		$info=json_decode($info,true);




		if($info['code']!='10000'){
			return $info['msg'];
		}
		

		//扣除货存
		$save['num']=$shopinfo['num']-$num;
		$where['id']=$shopid;
		$shopdb->where($where)->update($save);


		//创建订单
		$order=Db::name('order');
		$data['orderid']='cmorder'.date('YmdHis',time()).substr($user['telphone'],-4).rand(0,9);
		$data['ordertime']=date('Y-m-d H:i:s',time());
		$data['state']='1';//已扣积分
		$data['userid']=$user['id'];
		$data['type']='3';
		$data['pay']=$shopinfo['pay'];
		$data['shopid']=$shopinfo['id'];
		$data['shoptitle']=$shopinfo['title'];
		$data['num']='1';
		$data['remake']='1';
		for ($i=0; $i < $num; $i++) { 
			$data['key']=time().substr($user['telphone'],-4).$i;
			$order -> insert($data);
		}




		$tel=substr($user['telphone'],0,3).'****'.substr($user['telphone'],-4);
		$data = array(
		    'first' => array(
		        'value' => '尊敬的'.$user['wxname'].'： 您账号'.$tel.'的积分最新变动信息:',
		        'color' => '#FF0000'
		    ),
		    'keyword1' => array(
		        'value' => date('Y-m-d H:i:s',time()),
		        'color' => '#000'
		    ),
		    'keyword2' => array(
		        'value' => $shopinfo['title'],
		        'color' => '#000'
		    ),
		    'keyword3' => array(
		        'value' => $take_out,
		        'color' => '#000'
		    ),
		    'remark' => array(
		        'value' => '您已兑换成功，如有问题，请联系我们客服，点击查看详情。',
		        'color' => '#000'
		    )
		);
		$wxurl='http://www.sxjhol.vip/index.php';
		send_wx($data,$user['openid'],'m8gPbAJwLjxTpSF2WPk5pruZgqsfJfvOuSlwGufumNs',$wxurl);













		return 1;


	}

}